
package com.xoa.service.party;

import com.xoa.dao.party.VisitRecordsMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.VisitRecords;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 程叶同 on 2018/1/10.
 */
@Service
public class VisitRecordService {
    @Autowired
    VisitRecordsMapper visitRecordsMapper;

    /**
    * @Author 程叶同
    * @Description 添加记录
    * @Date 14:03 2018/1/10
    */

    public BaseWrapper addRecords(VisitRecords visitRecords){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(visitRecords!=null){
            int insert = visitRecordsMapper.insert(visitRecords);
            if(insert>0){
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("添加成功");
            }
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 修改记录
    * @Date 14:03 2018/1/10
    */

    public BaseWrapper updateRecords(VisitRecords visitRecords){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(visitRecords!=null){
            int i = visitRecordsMapper.updateByPrimaryKey(visitRecords);
            if(i>0){
                baseWrapper.setMsg("修改成功");
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
            }
        }
        return baseWrapper;
    }
    /**
    * @Author 程叶同
    * @Description 通过id查询
    * @Date 14:10 2018/1/10
    */

    public BaseWrapper getRecordsById(HttpServletRequest request, Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        if(id!=null&&id!=0){
            VisitRecords visitRecords = visitRecordsMapper.selectByPrimaryKey(id);
            List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(visitRecords.getImageName(), visitRecords.getImageId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
            visitRecords.setAttachmentList(attachments);
            if(visitRecords!=null){
                baseWrapper.setData(visitRecords);
                baseWrapper.setMsg("查询成功");
                baseWrapper.setFlag(true);
                baseWrapper.setStatus(true);
            }
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 查询全部
    * @Date 14:10 2018/1/10
    */
    public BaseWrapper getList(HttpServletRequest request, Integer page, Integer pageSize, String startTime, String endTime, String unitName){
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        BaseWrapper baseWrapper=new BaseWrapper();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("pageParams",pageParams);
        map.put("startTime",startTime);
        map.put("endTIme",endTime);
        map.put("unitName",unitName);
        map1.put("startTime",startTime);
        map1.put("endTIme",endTime);
        map1.put("unitName",unitName);
        List<VisitRecords> visitRecords = visitRecordsMapper.selectList(map);
        int i = visitRecordsMapper.totalItems(map1);
        if(visitRecords!=null){
            for(VisitRecords vr:visitRecords){
                List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(vr.getImageName(), vr.getImageId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                vr.setAttachmentList(attachments);
            }
            baseWrapper.setStatus(true);
            baseWrapper.setData(visitRecords);
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("ok");
            baseWrapper.setTotle(i);
        }
        return  baseWrapper;
    }

}
